var showInvest = function () {
    $('.tipsbox_bg').fadeIn().height($(document).height());
    $('.investbox').stop().animate({
        'bottom': '0'
    }, 300, function () {
        $("#invest_money").focus();
    });
};

var closeInvest = function () {
    $('.tipsbox_bg').fadeOut();
    $('.investbox').stop().animate({
        'bottom': '-100%'
    }, 300);
};

var checkInvestMoney = function (e) {
    e.value = e.value.replace(/\b(0+)/gi, '');
    e.value = e.value.replace(/[^\d]/gi, '');
    if (e.value > parseFloat($('#need').html())) {
        e.value = parseInt($('#need').html());
    }
};

var recheckmoney = function (money) {
    if (money > account) {
        layer.open({
            content: "尊敬的用户，您准备投标" + money + "元<br/><span class='af90'>但您的账户可用余额为" + account + "元，您要先去充值吗？</span>",
            btn: ['确定', '关闭'],
            yes: function (index) {
                location.href = '/user/recharge/index';
            }
        });
        return false;
    }
    if (money < invest_min) {
        wjtips("本标最低投标金额为" + invest_min + "元");
        return false;
    }
    if (money > invest_max && invest_max != 0) {
        wjtips("本标最大投标总金额为" + invest_max + "元");
        return false;
    }
    if (money > (need - invest_min) && (need - money) != 0) {
        layer.open({
            content: "最小投标金额不得低于50元，为确保满标，请输入小于或等于" + (need - invest_min) + "元或直接满标。  <br/><span class='af90'>是否帮您输入 " + need + " 元进行投资？</span>",
            btn: ['确定', '关闭'],
            yes: function (index) {
                $('#invest_money').val(need);
                layer.close(index);
            }
        });
        return false;
    }
    return true;
};

var invest = function (param, ajaxurl) {
    $.ajax({
        url: ajaxurl,
        type: "post",
        dataType: "json",
        data: param,
        success: function (d) {
            layer.closeAll();
            if (d.code == 1) {
                if ($("#toInvest").length > 0) {
                    showInvest_h5(param.money);
                } else {
                    wjtips(d.msg);
                    setTimeout("location.reload();", 1500);
                }
            } else {
                wjtips(d.msg);
                setTimeout(function () {
                    window.location.reload();
                }, 1500);
            }
        }
    });
};

/**
 *    红包使用
 */
var alertcouponlimit = function (m, e) {
    var couponchangemoney = function () {
        selectcoupon(e);
        $('#invest_money').val(parseFloat(m));
    };
    var t1 = ($(e).attr("data-type") == 2 ? '加息券' : '红包');
    var txt = '选择使用此' + t1 + '需要投资' + m + '元，当前输入投资金额不足，是否投资' + m + '元使用此' + t1 + '？';
    layer.open({
        content: txt,
        btn: ['确定', '关闭'],
        yes: function (index) {
            couponchangemoney();
            layer.close(index);
        }
    });
};

var selectcoupon = function (e) {
    var type = $(e).attr("data-type");
    var text = '使用 ' + $(e).attr('data-money') + (type == 2 ? '%加息券' : '元红包');
    var day = $(e).attr("data-day");
    if (type == 2 && day > 0) {
        $("#plus_txt").show();
    } else {
        $("#plus_txt").hide()
    }
    $('#card_id').val($(e).attr('data-id'));
    $('.usercouponbtm span').html(text);
    movebot('coupon_frame');
};

// 倒计时
var countdown = function (over) {
    var seconds;
    var timer = null;

    function showSeconds() {
        var day1 = Math.floor(seconds / (60 * 60 * 24));
        var hour = Math.floor((seconds - day1 * 24 * 60 * 60) / 3600);
        var minute = Math.floor((seconds - day1 * 24 * 60 * 60 - hour * 3600) / 60);
        var second = Math.floor(seconds - day1 * 24 * 60 * 60 - hour * 3600 - minute * 60);
        if (day1 < 0) {
            clearInterval(timer);
            $("#investFrist").addClass("dis").html('已结束').attr("href", "javascript:void(0);");
        }
        seconds--;
    }

    var now = Math.round(new Date() / 1000); //当前的时间戳
    seconds = parseInt((over - now), 10); //当前剩余时间戳
    timer = setInterval(showSeconds, 1000);
};

$(function () {
    if ($('.cardlist ul').html() == '') {
        $(this).hide().parent().find('.empty').show();
    }

    $('#cardlist li').on('click', function () {
        var invest_money = parseFloat($('#invest_money').val()) ? parseFloat($('#invest_money').val()) : '0';
        if (parseFloat($(this).attr('data-limitmoney')) > invest_money) {
            alertcouponlimit($(this).attr('data-limitmoney'), $(this));
        } else {
            selectcoupon($(this));
        }
    });
});